package com.example.mobile2121606109;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

    private CheckBox chkboxMainYinqing;
    private CheckBox chkboxMainBoli;
    private CheckBox chkboxMainFangxiangpan;
    private Button btnMainBuy;
    private TextView tvMainResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        chkboxMainYinqing.setOnCheckedChangeListener(this);
        chkboxMainBoli.setOnCheckedChangeListener(this);
        chkboxMainFangxiangpan.setOnCheckedChangeListener(this);

        btnMainBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String msg = "选中：";
                if (chkboxMainYinqing.isChecked()){
                    msg += "\n" + chkboxMainYinqing.getText().toString();
                }
                if (chkboxMainBoli.isChecked()) {
                    msg += "\n" + chkboxMainBoli.getText().toString();
                }
                if (chkboxMainFangxiangpan.isChecked()) {
                    msg += "\n" + chkboxMainFangxiangpan.getText().toString();
                }
                tvMainResult.setText(msg);
            }
        });
    }

    private void initView() {
        chkboxMainYinqing = (CheckBox) findViewById(R.id.chkboxMainYinqing);
        chkboxMainBoli = (CheckBox) findViewById(R.id.chkboxMainBoli);
        chkboxMainFangxiangpan = (CheckBox) findViewById(R.id.chkboxMainFangxiangpan);
        btnMainBuy = (Button) findViewById(R.id.btnMainBuy);
        tvMainResult = (TextView) findViewById(R.id.tvMainResult);
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        if (b) {
            tvMainResult.setText("选中：" + compoundButton.getText().toString());
        }else{
            tvMainResult.setText("取消选中：" + compoundButton.getText().toString());
        }
    }
}